package cn.tedu.ximart.server.service.impl;


import cn.tedu.ximart.server.ex.ServiceException;
import cn.tedu.ximart.server.mapper.CategoryMapper;
import cn.tedu.ximart.server.pojo.dto.CategoryAddNewDTO;
import cn.tedu.ximart.server.pojo.entity.Category;
import cn.tedu.ximart.server.pojo.vo.CategoryListItemVO;
import cn.tedu.ximart.server.repo.ICategoryRepository;
import cn.tedu.ximart.server.service.ICategoryService;
import cn.tedu.ximart.server.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class CategoryServiceImpl implements ICategoryService {

    @Autowired
    private ICategoryRepository categoryRepository;

    @Autowired
    private CategoryMapper mapper;


    @Override
    public void addNew(CategoryAddNewDTO categoryAddNewDTO) {
        log.debug("准备开始处理增加品牌的业务，参数：{}",categoryAddNewDTO);

        String name = categoryAddNewDTO.getName();
        int count = mapper.countByName(name);
        if(count>0){
            String message = "增加分类失败，分类名称【"+name+"】已经被占用";
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
        Category category = new Category();
        BeanUtils.copyProperties(categoryAddNewDTO,category);
        int rows = mapper.insert(category);
        if(rows!=1){
            String message = "添加分类失败，服务器繁忙，请稍后再尝试";
            log.error(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }

    }

    @Override
    public void deleteById(Long id) {

    }

    @Override
    public List<CategoryListItemVO> getCategories() {
        return null;
    }

}
